import { Injectable } from '@angular/core';
import { Events } from '@ionic/angular';
@Injectable({
  providedIn: 'root'
})
export class EventService {

  constructor(private event:Events) { }
  /**
   * 注册事件
   * @param eventName 注册事件的名称
   * @param callback  通过回调函数去获取里面的值，并进行逻辑处理
   * @param isUnsubscribe true是清除注册,false不清除直接注册
   * @param args 注册后回传过来的参数
   */
  subscribe(eventName: any, callback: any, isUnsubscribe?: Boolean, ...args: any[]) {
    if (isUnsubscribe) {
      this.event.unsubscribe(eventName);
    }
    this.event.subscribe(eventName, (...args) => {
      callback(...args);
    });
  }
  /**
   * 事件触发
   */
  publish(eventName: any,...args: any[]){
    this.event.publish(eventName,...args);
  }

  unsubscribe(eventName: any){
    this.event.unsubscribe(eventName);
  }
}
